
finp2$="picture.png"

shell("convert "+finp2$+" -resize 320x200\\! _tmpr2.jpg")
finp$="_tmpr2.jpg"

dim cr[40,25,16],mt[40,25],cmu[16],ag[40,25,4],iklv[16],colourid[16],colourid2[16],pke[16384]

'- c64 hires picture converter - sdlBasic version - Paulo Silva - august'02 (Amos) -> october'07 (sdlBasic) - GPL licence

' cr[,,] - ammount of pixels from each colour in each attribute
' mt[,]  - ammount of colours in each attribute
' cmu[]  - ammount of attributes with 4 colours or more which each colour belongs
' cnt    - most used colour in attributes with 4 colours or more
' ag[,,] - attributes

xed=640:yed=400:wdwd=1
setdisplay(xed,yed,32,wdwd)
paper(0xFFFFFF):ink(0):cls

colourid[0]=0x000:colourid[1]=0xFFF:colourid[2]=0x931:colourid[3]=0x5BD
colourid[4]=0x93C:colourid[5]=0x4A1:colourid[6]=0x32C:colourid[7]=0xBD3
colourid[8]=0x950:colourid[9]=0x440:colourid[10]=0xC64:colourid[11]=0x444
colourid[12]=0x777:colourid[13]=0x8E5:colourid[14]=0x75F:colourid[15]=0x999

for i=0 to 15
  u=colourid[i]
  rlv=(int(u/256))
  glv=bitwiseand((int(u/16)),15)
  blv=bitwiseand(u,15)
  iklv[i]=((blv*11)+(glv*59)+(rlv*30))/15
  next
for i=0 to 15
  u=colourid[i]
  rlv=(int(u/256))
  glv=bitwiseand((int(u/16)),15)
  blv=bitwiseand(u,15)
  colourid2[i]=(int((rlv/15)*255))*65536+(int((glv/15)*255))*256+(int((blv/15)*255))
  next

loadimage(finp$,1):pasteicon(0,0,1)

' ----- step 1
' ----- counts ammount of pixels from each colour from each attribute
for y1=0 to 24
  setcaption("step 1: "+str$(y1)+"/24     ")
  for x1=0 to 39
    for y2=0 to 7:for x2=0 to 7
      x=x2+(x1*8)
      y=y2+(y1*8)
      u2=point(x,y)
      u=0:dis=1000
      for i=0 to 15
        qq=colourid[i]
        blv=qq mod 16:qq=int(qq/16)
        glv=qq mod 16:qq=int(qq/16)
        rlv=qq mod 16
        qq=u2
        blv2=int(qq/16) mod 16:qq=int(qq/256)
        glv2=int(qq/16) mod 16:qq=int(qq/256)
        rlv2=int(qq/16) mod 16
        bdist=abs(blv-blv2)
        gdist=abs(glv-glv2)
        rdist=abs(rlv-rlv2)
        rgbdist=sqr((rdist^2)+(gdist^2)+(bdist^2))
        if rgbdist<dis then:dis=rgbdist:u=i:end if
        next
      cr[x1,y1,u]=cr[x1,y1,u]+1
      next:next:next:next

' ----- step 2
' ----- counts ammount of colours in each attribute
for y1=0 to 24
  setcaption("step 2: "+str$(y1)+"/24     ")
  for x1=0 to 39
    es=0
    for e=0 to 15
      if cr[x1,y1,e]<>0 then:es=es+1:end if
      next
    mt[x1,y1]=es
    next:next

' ----- step 3
' ----- counts the most used colours in attributes with 2 colours or more
for y1=0 to 24
  setcaption("step 3: "+str$(y1)+"/24     ")
  for x1=0 to 39
    if mt[x1,y1]>=2 then
      for e=0 to 15
        if cr[x1,y1,e]>0 then:cmu[e]=cmu[e]+1:end if 
        next
      end if 
    next:next

' ----- step 4
' ----- sets index from 1 to 2 from each attribute with remaining colours
for y1=0 to 24
  setcaption("step 4: "+str$(y1)+"/24")
  for x1=0 to 39
    for g=1 to 2
      mx=0:ct=0
      for e=0 to 15
        if cr[x1,y1,e]>mx then:ct=e : mx=cr[x1,y1,e]: end if 
        next
      cr[x1,y1,ct]=0 : ag[x1,y1,g]=ct
      next:next:next

' ----- step 5
' ----- clears the value 16 created from the step above (becomes background0)  
for y1=0 to 24
  setcaption("step 5: "+str$(y1)+"/24")
  for x1=0 to 39
    for g=1 to 2
      if ag[x1,y1,g]>15 then:ag[x1,y1,g]=ag[0,0,0]:end if 
   next:next:next

' ----- step 6
' ----- i can't remember...
ink (colourid2[ag[0,0,0]]):bar(320,0,639,199)
for y1=0 to 24
  setcaption("step 6: "+str$(y1)+"/24")
  for x1=0 to 39
    for e=1 to 2
      ink (colourid2[ag[x1,y1,e]])
      px=320+(x1*8)+1
      py=(y1*8)+((e-1)*2)+1
      bar(px,py,px+5,py+1)
      next:next:next

' ----- step 7
' ----- i can't remember... (binaries?)
ink (colourid2[ag[0,0,0]]):bar(0,200,319,399)
for y1=0 to 24
  setcaption("step 7: "+str$(y1)+"/24")
  for x1=0 to 39
    kv1=iklv[ag[x1,y1,1]]:kv2=iklv[ag[x1,y1,2]]
    ink (colourid2[ag[x1,y1,1]])
    for y2=0 to 7
      vac=0
      for x2=0 to 7
        x=(x1*8)+x2
        y=(y1*8)+y2
        u2=point(x,y)
        u=0:dis=1000
        for i=0 to 15
          qq=colourid[i]
          blv=qq mod 16:qq=int(qq/16)
          glv=qq mod 16:qq=int(qq/16)
          rlv=qq mod 16
          qq=u2
          blv2=int(qq/16) mod 16:qq=int(qq/256)
          glv2=int(qq/16) mod 16:qq=int(qq/256)
          rlv2=int(qq/16) mod 16
          bdist=abs(blv-blv2)
          gdist=abs(glv-glv2)
          rdist=abs(rlv-rlv2)
          rgbdist=sqr((rdist^2)+(gdist^2)+(bdist^2))
          if rgbdist<dis then:dis=rgbdist:u=i:end if
          next
        kvv=iklv[u]
        ink (colourid2[ag[x1,y1,1]]):dot(x,y+200)
        ink (colourid2[0]):dot(x+320,y+200)
        if abs(kv2-kvv)<abs(kv1-kvv) then: 
          ink(colourid2[ag[x1,y1,2]]):dot(x,y+200)
          ink(colourid2[1]):dot(x+320,y+200)
          vac=vac+(2^(7-x2))
          end if 
      next
      adrp=0+2+y2+(x1*8)+(y1*320): pke[adrp]=vac
next:next:next

' ----- step 8
' ----- i can't remember... (binaries?)
for y1=0 to 24
  setcaption("step 8: "+str$(y1)+"/24")
  for x1=0 to 39
    adrp=0+x1+(y1*40)
    pke[adrp+8002]=((ag[x1,y1,2])*16)+ag[x1,y1,1]
    next:next
  pke[0+9002]=0:pke[0+9003]=ag[0,0,0]
  pke[0+0]=0:pke[0+1]=32

' ----- step 9
' ----- saving
  setcaption("step 9: saving...")
open finp$+".hpic" for output as #1
for i=0 to 9009
  writebyte(1,pke[i])
  next
close #1

grab (1,0,200,320,200):saveimage(finp$+"_.bmp",1)

'- this line below runs on Linux with Imagemagick installed
shell("convert "+finp$+"_.bmp"+" "+finp$+"_.png"):shell("rm "+finp$+"_.bmp")

setcaption("ready!")
waitkey

